<!--
 * @Author: tkp
 * @Date: 2024-06-06 10:22:02
-->
<template>
  <div>
    <el-form :model="queryForm" label-position="right" :rules="rules" ref="ruleForm" label-width="120px">
      <el-form-item prop="adminName" label="名称:">
        <Input v-model="queryForm.adminName" disabled size="medium" clearable placeholder="请输入名称"></Input>
      </el-form-item>
      <el-form-item prop="roleIds" label="配置角色:">
        <Select size="medium" placeholder="请选择角色" multiple :clearable="false" v-model="queryForm.roleIds" :data="roleList" :jsonType="{'value':'roleId','label':'roleName'}"></Select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="medium" @click="okPop">确认</el-button>
        <el-button size="medium" @click="closePop">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { authRoleQuery, authoritySelectedRoles } from '@/utils/api'
export default {
  props: {
    data: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      queryForm: {
        roleIds: [],
        sysAdminId: '',
        adminName: '',
      },
      roleList: [],
      rules: {
        adminName: [
          { required: true, message: '此项为必填' },
        ],
        roleIds: [
          { required: true, message: '此项为必填' },
        ],
      },
    }
  },
  mounted() {
    this.queryForm.sysAdminId = this.data.sysAdminId
    this.queryForm.adminName = this.data.adminName
    this.queryForm.roleIds = this.data.roleIds
    this.load()
    this.loadAuthRole()
  },
  methods: {
    closePop() {
      this.$emit('roleData', '')
    },
    okPop() {
      let arr = []
      this.queryForm.roleIds.forEach(item => {
        let param = {
          operatorId: this.queryForm.sysAdminId,
          roleId: item
        }
        arr.push(param)
      })
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.$emit('roleData', arr)
        } else {
          this.messageWarnPop('请填写完整')
          return false;
        }
      });
    },
    load() {
      let param = {
        enable: 1,
      }
      authRoleQuery(param).then(res => {
        this.roleList = res.data
      })
    },
    loadAuthRole() {
      authoritySelectedRoles(this.queryForm.sysAdminId).then(res => {
        let data = res.data || []
        this.queryForm.roleIds = []
        data.forEach(item => {
          this.queryForm.roleIds.push(item.roleId)
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
